package com.awonsome.example.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author yangdejun
 * @date 2022/1/7 11:38
 */
@RestController
@RequestMapping(value = "/oss")
public class SimpleUpload {

    @Autowired
    private OSS oss;

    /**
     * 简单上传
     *
     * @throws IOException
     */
    @GetMapping(value = "/upload/simple")
    public void simpleUpload() throws IOException {
        String content = "Hello OSS";
        // 流式上传 - 上传字符串
        System.out.println("流式上传 - 上传字符");
        PutObjectRequest putObjectRequest = new PutObjectRequest("yangdj", "hello.txt", new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
        oss.putObject(putObjectRequest);
        // 流式上传 - 上传 byte 数组
        System.out.println("流式上传 - 上传 byte 数组");
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        oss.putObject("yangdj", "hello2.txt", new ByteArrayInputStream(bytes));
        // 流式上传 - 上传网络流
        System.out.println("流式上传 - 上传网络流");
        InputStream inputStream = new URL("https://yangdj.oss-cn-zhangjiakou.aliyuncs.com/2022/01/01/00/00_1640966409038531641_90347569").openStream();
        oss.putObject("yangdj", "hello3.txt", inputStream);
        // 流式上传 - 上传文件流
        System.out.println("流式上传 - 上传文件流");
        InputStream inputStream1 = new FileInputStream("/Users/yangdejun/IdeaProjects/personal/awonsome-tools/example/src/main/java/com/awonsome/example/oss/hello2.txt");
        oss.putObject("yangdj", "hello4.txt", inputStream1);
        // 文件上传
        System.out.println("文件流上传");
        oss.putObject("yangdj", "hello5.txt", new File("/Users/yangdejun/IdeaProjects/personal/awonsome-tools/example/src/main/java/com/awonsome/example/oss/hello2.txt"));
    }

    /**
     * 表单上传
     */
    @GetMapping(value = "/upload/form")
    public void formUpload() {
        String localFilePath = "";

    }

    /**
     * 追加上传
     */
    @GetMapping(value = "/upload/append")
    public void appendUpload() {

    }

    /**
     * 端点续传
     */
    @GetMapping(value = "/upload/dt")
    public void dtUpload() {

    }

    /**
     * 分片上传
     */
    @GetMapping(value = "/upload/slice")
    public void sliceUpload() {

    }

    /**
     * 进度条
     */
    @GetMapping(value = "/upload/progress")
    public void progressUpload() {

    }

    /**
     * 上传回调
     */
    @GetMapping(value = "/upload/callback")
    public void uploadCallback() {

    }
}
